package com.funkyfurrymonkey.load ;

import haxe.ds.HashMap;
import haxe.xml.Fast;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Sprite;
import openfl.events.Event;

/**
 * ...
 * @author Marcos Martinez
 */
class Preloader extends Sprite
{
	private var m_cPreloaderScreen:PreloaderScreen;
	private var m_nPreloaderTotal:Int;
	private var m_nPreloaderCurrent:Int;
	private var m_cXml:Fast;
	public var m_cAssetsRepo:AssetsRepository;
	private var m_aImageLoader:Map<String,ImageLoader>;
	private var m_nTotalAssets:Int;
	private var m_nCurrentAssets:Int;

	public function new(cMainCanvas:Sprite, cAssetsRepo:AssetsRepository) 
	{
		super();
		cMainCanvas.addChild(this);
		m_cPreloaderScreen = new PreloaderScreen();
		this.addChild(m_cPreloaderScreen);
		m_cAssetsRepo = cAssetsRepo;
	}
	
	public function start(sXmlName:String):Void
	{
		var cXmlLoader:DataLoader = new DataLoader(sXmlName);
		cXmlLoader.onComplete = onXmlLoaded;
		cXmlLoader.start();
	}
	
	private function onXmlLoaded(cData:Dynamic):Void
	{
		var cXml:Xml =  Xml.parse(cData.data);
		m_cXml = new Fast(cXml);
		
		var cPreXml:Fast	= m_cXml.node.resources.node.preloader;
		
		//number of element to download from preload
		m_nPreloaderTotal = 0;
		m_nPreloaderCurrent = 0;
		for(ech in cPreXml.elements)
			m_nPreloaderTotal++;
		
		var cPreloaderBackground:ImageLoader = new ImageLoader(cPreXml.node.background.att.file);
		cPreloaderBackground.onComplete = function (bmd:BitmapData)
		{
			var bm:Bitmap = new Bitmap(bmd);
			m_cPreloaderScreen.setBackground(bm);
			checkPreloadeReady();
		};
		cPreloaderBackground.start();
		
		var cPreloaderBar:ImageLoader = new ImageLoader(cPreXml.node.progressbar.att.file);
		cPreloaderBar.onComplete = function (bmd:BitmapData)
		{
			var bm:Bitmap = new Bitmap(bmd);
			m_cPreloaderScreen.setProgressBar(bm);
			checkPreloadeReady();
		};
		cPreloaderBar.start();
	}
	
	private function checkPreloadeReady():Void
	{
		m_nPreloaderCurrent++;
		if (m_nPreloaderCurrent != m_nPreloaderTotal)
			return;
		m_cPreloaderScreen.setScreen();
		readingAssetsFromXmls();
	}
	
	private function readingAssetsFromXmls():Void
	{
		trace ("start getting assets");
		m_nTotalAssets = 0;
		m_nCurrentAssets = 0;
		getImagesFromAssetsXml();
		getSoundFromAssetsXml();
		getXmlsFromAssetsXml();
		startDownloading();
	
	}
	

	
	private function getImagesFromAssetsXml():Void
	{
		m_aImageLoader = new Map<String, ImageLoader>();
		var xImgs:Fast = m_cXml.node.resources.node.images;
		for (img in xImgs.elements)
		{
			m_nTotalAssets++;
			var cImgLoader:ImageLoader = new ImageLoader(img.att.file);
			m_aImageLoader.set(img.name, cImgLoader);
			cImgLoader.onComplete = function (bmd:BitmapData)
			{
				trace("image loaded :" + img.name);
				m_nCurrentAssets++;
				m_cAssetsRepo.addImage(img.name , new Bitmap( bmd));
				checkFinish();
			}
		}
		
	}

	private function getSoundFromAssetsXml():Void
	{
	}
	
	private function getXmlsFromAssetsXml():Void
	{
	}
	
	private function startDownloading():Void
	{
		for (img in m_aImageLoader)
		{
			img.start();
		}
	}
	
	private function checkFinish():Void
	{
		if (m_nCurrentAssets != m_nTotalAssets)
			return;
		onComplete();
			
	}
	
	dynamic public function onComplete() { }
	
	public function removePreloaderScreen()
	{
		while (this.numChildren > 0)
			this.removeChildAt(0);
			
	}
	
}